<template>
    <div class="content">
        <div class="query" v-if="showQuery">
            <el-form :inline="true" :model="form" label-width="auto">
                <el-form-item label="流程名称：">
                    <el-input clearable v-model="form.definitionNm" style="width: 200px" placeholder="请输入流程名称查询" />
                </el-form-item>
                <el-form-item label="状态：">
                    <el-select clearable v-model="form.status" placeholder="请选择状态查询" style="width: 200px">
                        <el-option label="开启" value="0" />
                        <el-option label="关闭" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="200">
                    <el-button type="primary" @click="getData" :icon="Search">查询</el-button>
                    <el-button @click="reset" :icon="Refresh">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="fun">
            <div class="left">
                <el-button type="primary"
                    @click="router.push({ path: '/workflow/configure', query: { disabled: false } })" plain
                    :icon="Plus">新增</el-button>
            </div>
            <div class="right">
                <el-tooltip :content="showQuery ? '隐藏搜索栏' : '展示搜索栏'" placement="top">
                    <el-button :icon="TurnOff" circle @click="showQuery = false" v-if="showQuery" />
                    <el-button :icon="Open" circle @click="showQuery = true" v-else />
                </el-tooltip>
                <el-tooltip content="刷新" placement="top">
                    <el-button :icon="Refresh" circle @click="getData()" />
                </el-tooltip>
            </div>
        </div>
        <div class="table">
            <el-table empty-text="暂无数据" :show-overflow-tooltip="true" :data="tableData" v-loading="loading"
                element-loading-text="数据加载中..."
                :header-cell-style="{ background: 'var(--table-title-color)', color: 'var(--table-title-font-color)' }"
                @selection-change="handleSelectionChange" border>
                <el-table-column type="selection" width="55" />
                <el-table-column prop="wfNo" label="流程编码" />
                <el-table-column prop="definitionNm" label="流程名称" />
                <el-table-column prop="version" label="版本">
                    <template #default="{ row }">
                        {{ 'v' + row.version.toFixed(1) }}
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="流程说明" />
                <el-table-column prop="status" label="状态">
                    <template #default="scope">
                        <el-switch v-model="scope.row.status" :active-value="'0'" :inactive-value="'1'" inline-prompt
                            active-text="开启" inactive-text="关闭"
                            @change="changeStatus(scope.row.id, scope.row.status)" />
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" />
                <el-table-column :fixed="$isMobile() ? false : 'right'" label="操作" width="250" column-align="center">
                    <template #default="scope">
                        <el-button link type="primary" size="small" :icon="Pointer" @click="initiate(scope.row.id)"
                            v-if="scope.row.status === '0'">发起流程</el-button>
                        <el-button text type="primary" size="small" :icon="Share"
                            @click="toVersion(scope.row.wfNo)">版本管理</el-button>
                        <el-dropdown>
                            <span
                                style="font-size: 12px; cursor: pointer;color: #409EFF;margin-left: 10px; margin-top: 5px;">
                                更多
                                <el-icon>
                                    <arrow-down />
                                </el-icon>
                            </span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item
                                        @click="router.push({ path: '/workflow/configure', query: { id: scope.row.id, disabled: true } })">
                                        详情
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="router.push({ path: '/workflow/configure', query: { id: scope.row.id, disabled: false } })">
                                        修改
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-config-provider>
            <el-pagination v-model:current-page="page.currentPage" v-model:page-size="page.pageSize"
                :page-sizes="[10, 20, 30, 40]" :pager-count="$isMobile() ? 5 : 7"
                :layout="$isMobile() ? 'prev,pager,next' : 'total, sizes, prev, pager, next, jumper'"
                :background="!$isMobile()" :total="page.total" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
        </el-config-provider>
        <workflowFillData ref="fill"></workflowFillData>
        <wfVersion ref="version" @serversUpdated="getData"></wfVersion>
    </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useRouter } from 'vue-router';
import api from "@/api"
import { Pointer, Search, Plus, TurnOff, Open, Refresh, Share } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import workflowFillData from "../common/workflowFillData.vue";
import wfVersion from "./wfVersion.vue"

const showQuery = ref(true)
const router = useRouter();
const loading = ref(false)
const form = ref({ enableFlag: '0' })
const page = ref({ pageSize: 10, pageNum: 1, currentPage: 0, total: 0 })
const tableData = ref([])
const ids = ref([])
const status = ref()
const fill = ref()
const version = ref()

//获取数据
const getData = async () => {
    loading.value = true
    let params = { pageNum: page.value.pageNum, pageSize: page.value.pageSize, ...form.value }
    const res = await api.get('/workflow/definition/pageList', params)
    if (res.data.code === 200) {
        tableData.value = res.data.data.records
        page.value.currentPage = res.data.data.current
        page.value.total = res.data.data.total
    } else {
        ElMessage.error(res.data.msg)
    }
    loading.value = false
}
//切换页
const handleSizeChange = (val) => {
    page.value.pageSize = val
    getData()
}
const handleCurrentChange = (val) => {
    page.value.pageNum = val
    getData()
}
//删除
const deleteData = (id) => {
    let params
    if (id !== 'batch') {
        params = [id]
    } else {
        if (ids.value.length === 0) {
            ElMessage.warning('请先选择需要删除的行，再做操作!')
            return
        }
        params = ids.value
    }
    api.post('/workflow/definition/delete', params).then(res => {
        if (res.data.code === 200) {
            ElMessage.success(res.data.msg)
            getData()
        } else {
            ElMessage.error(res.data.msg)
        }
    })
}
//重置查询条件
const reset = () => {
    status.value = null
    form.value = { enableFlag: '0' }
    getData()
}
//修改状态
const changeStatus = async (id, status) => {
    const res = await api.post('/workflow/definition/changeStatus', { id, status })
    if (res.data.code === 200) {
        ElMessage.success(res.data.msg)
    } else {
        ElMessage.error(res.data.msg)
    }
}
const handleSelectionChange = (val) => {
    let idArr = val.map(item => item.id)
    ids.value = idArr
}
//发起流程
const initiate = (id) => {
    fill.value.init(id, null)
}
const toVersion = (wfNo) => {
    version.value.init(wfNo)
}
onMounted(() => {
    getData()
})
</script>
<style scoped lang="less"></style>